<?
/**
*  账号管理
*  2013/7/14 by zh
*/
include("database.php");

include("session.php");
require("ajax_base.php");

$action = $_GET['action'];
$table_name = "qc_user";

if($action == "del") {
	$db->doDelete($table_name, 'id='.$_GET['id']); 
	echo "ok";

} else if($action == "list") {
	$condition = "1=1";
	if(isset($_GET['user_status']) && $_GET['user_status']!=0) {
		$condition .= " and user_status=".$_GET['user_status'];
	}
	if(isset($_GET['username'])) {
		$condition .= " and user_name='".$_GET['username']."'";
	}
	if(isset($_GET['telephone'])) {
		$condition .= " and contact_tel='".$_GET['telephone']."'";
	}
	$sql = "select id,user_name,true_name,contact_tel,user_flag, user_status, callout_number, callable_count, '' as do_charge, register_time, last_login, login_count from $table_name where $condition order by id desc"; 
	echo to_grid_json($db, $sql, $dic_data, array("user_name"=>"blue_color_mapper","contact_tel"=>"red_color_mapper", "do_charge"=>"do_charge_mapper"));

} else if($action == "get") {
	$sql = "select * from $table_name where id=".$_GET['id']; 
	$arr = $db->getOneRecordArray($sql);
	echo array_to_json($arr);

} else if($action == "get_by_uid") {
	$sql = "select * from $table_name where id=".$_SESSION['user_id']; 
	$arr = $db->getOneRecordArray($sql);
	echo array_to_json($arr);

} else if($action == "add") {
	if($db->getOneRecord("select id from $table_name where user_name='".$_POST['user_name']."'") != null) {
		echo 'username_exists';
		exit;
	}
	unset($_POST['id']);
	$_POST['register_time'] = date("Y-m-d H:i:s");
	$db->doInsert($table_name, $_POST);
	echo 'ok';

} else if($action == "edit") {
	$id = $_POST['id'];
	unset($_POST['id']);
	$db->doUpdate($table_name, $_POST, 'id='.$id);
	echo 'ok';

} else if($action == "modify_pwd") {
	$sql = "select user_pass from $table_name where user_name='".$_SESSION['user_name']."'"; 
	$arr = $db->getOneRecordArray($sql);
	if($arr['user_pass'] != $_POST['o_user_pass']) {
		die("o_userpass_incorrect");
	}
	$db->doUpdate($table_name, array("user_pass"=>$_POST['n_user_pass']), "user_name='".$_SESSION['user_name']."'");
	echo 'ok';

} 


function do_charge_mapper($v, $arr) {
	return "(<a href=\"javascript:do_charge(".$arr['id'].")\">充值</a>)";
}

?>